/**
 * gislin 2017年7月11日
 */
package com.btcode.db.executer;

import com.btcode.db.config.DataBaseConfig;
import com.btcode.db.config.DataBaseType;
import com.btcode.db.connection.AccessConnectionGetter;
import com.btcode.db.connection.DefaultConnectionGetter;
import com.btcode.db.connection.IConnectionGetter;
import com.btcode.db.connection.OracleConnectionGetter;
import com.btcode.db.connection.PostgreSQLConnectionGetter;
import com.btcode.exception.MsgException;

/**
 * @author gislin 2017年7月11日
 */
public class DefaultDataExecuterFactory implements IDataExecuterFactory {

    @Override
    public IDataExecuter getDataExecuter(DataBaseConfig dbConfig) {
        return getDataExecuter(dbConfig, null);
    }

    @Override
    public IDataExecuter getDataExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter) {

        try {
            if (dbConfig.getDbType().equals(DataBaseType.oracle)) {
                OracleConnectionGetter oracleConnectionGetter = new OracleConnectionGetter(dbConfig);
                return getOracleExecuter(dbConfig, oracleConnectionGetter);
            }
            else if (dbConfig.getDbType().equals(DataBaseType.postgre)) {
                PostgreSQLConnectionGetter postgreSQLConnectionGetter = new PostgreSQLConnectionGetter(dbConfig);
                return getPostgreSQLExecuter(dbConfig, postgreSQLConnectionGetter);
            }
            else if (dbConfig.getDbType().equals(DataBaseType.sqllet)) {
                connectionGetter = new DefaultConnectionGetter(dbConfig);
                return getSQLLetExecuter(dbConfig, connectionGetter);
            }
            else if (dbConfig.getDbType().equals(DataBaseType.mysql)) {
                connectionGetter = new DefaultConnectionGetter(dbConfig);
                return getMySQLExecuter(dbConfig, connectionGetter);
            }
            else if (dbConfig.getDbType().equals(DataBaseType.access)) {
                AccessConnectionGetter accessConnectionGetter = new AccessConnectionGetter(dbConfig);
                return getAccessExecuter(dbConfig, accessConnectionGetter);
            }
            else if(dbConfig.getDbType().equals(DataBaseType.mssql)){
                connectionGetter = new DefaultConnectionGetter(dbConfig);
                return getSQLServerExecuter(dbConfig, connectionGetter);
            }
            else if(dbConfig.getDbType().equals(DataBaseType.dm)){
                connectionGetter = new DefaultConnectionGetter(dbConfig);
                return getDmExecuter(dbConfig, connectionGetter);
            }
            else {
                throw new MsgException(dbConfig.getDbType() + "的数据库操作类还未实现");
            }
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private IDataExecuter getOracleExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter){
        return new OracleDataExecuter(dbConfig, connectionGetter);
    }

    private IDataExecuter getPostgreSQLExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter){
        return new PostgreSQLDataExecuter(dbConfig, connectionGetter);
    }

    private IDataExecuter getSQLServerExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter){
        return new SQLServerDataExecuter(dbConfig, connectionGetter);
    }

    private IDataExecuter getSQLLetExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter){
        return new SQLLetDataExecuter(dbConfig, connectionGetter);
    }

    private IDataExecuter getMySQLExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter){
        return new MySQLDataExecuter(dbConfig, connectionGetter);
    }

    private IDataExecuter getAccessExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter){
        return new AccessDataExecuter(dbConfig, connectionGetter);
    }

    private IDataExecuter getDmExecuter(DataBaseConfig dbConfig,
        IConnectionGetter connectionGetter){
        return new DmDataExecuter(dbConfig, connectionGetter);
    }
}
